home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / editor / numlines.zip / NUMLINES.ASM < prev    next >
Assembly Source File  |  1996-06-20  |  5KB  |  164 lines

  1. ; NumLines  Barry Block  v5 6-96 assemble with A86
  2.  
  3. .Radix 16
  4. buffer_size equ 0F400h            ;buffer size = 61k
  5. o        equ offset
  6. Org    100h
  7.         Jmp    start
  8.  
  9.         Db    1bh,5bh,32h,4ah,0dh
  10. brag        Db    'NumLines v5 by Barry Block  6-96  2:820/901.42'
  11.         Db    0Dh,0Ah,24h,0dh
  12. usage        Db    'Counts number of lines in a textfile.',0dh,0ah
  13.         Db    'Faster than 4DOS function, @lines[filename]',0dh,0ah
  14.         Db    'Usage:  Numlines Filename',0dh,0ah
  15.         Db    9,'Numlines Filename|input %%var',0dh,0ah
  16.         Db    24h,0dh,20h,0dh,1ah
  17. ;---------------------------------------
  18. start:        Call init
  19.         Call open_file
  20. _Do:        Call read_file
  21.         Call filter
  22.         Call status
  23.         Jmp  short _Do
  24.  
  25. ;---------------------------------------
  26. init:        Mov    si,82h        ;check CommandTail for filename
  27.         Cmp    b[si],0h    ;is it zero?
  28.         Jz     err_use        ;commandtail wrong
  29. loop1:        Lodsb
  30.         Cmp    al,0Dh
  31.         Jnz    loop1
  32.         Mov    w[si-1],0    ;replace CR (13) with 0 in the filename
  33.         Ret
  34. err_use:    Mov    b set_err,-1    ;no filename?
  35.         Jmp    short _q    ;clear ret off stack and quit
  36. ;---------------------------------------
  37. open_file:    Mov    dx,82h        ;point dx to filename
  38.         Mov    ax,3d00h    ;DOS:open file, DS:DX=name
  39.         Int    21h        ; AL=access code, 0 is read only
  40.         Jc    o_err        ; AX=error code if carry set
  41.         Mov    handle,ax    ;AX holds Handle
  42.         Ret
  43. o_err:        Mov    b set_err,-1
  44. _q:        pop    ax
  45.         Jmp    short quit
  46. ;---------------------------------------
  47. read_file:    Clc
  48.         Mov    bx,handle    ;DOS:move file ptr,
  49.         Xor    dx,Dx        ; AL=method BX=hnd
  50.         Mov    ax,4201h    ;CX:DX = #bytes,ret DX:AX=pos in file
  51.         Mov    cx,0
  52.         Int    21h
  53.         Mov    FilePtrH,Dx    ;Store current position
  54.         Mov    FilePtrL,Ax
  55.         Mov    dx,o buffer
  56.         Mov    cx,buffer_size    ;DOS:read file, BX=handle
  57.         Mov    ah,3fh        ; CX=# bytes,DS:DX=buffer
  58.         Int    21h        ; addr,ret AX=err code if carry
  59.         Mov    NumBytes,Ax
  60.         Jc    ReadErr
  61.         Cmp    ax,cx
  62.         Je    RFDone
  63. SetEOF:     Mov    b EOF,-1
  64.         Jmp    short RFDone
  65. ReadErr:    Cmp    ax,0
  66.         Je    SetEOF        ;no problem
  67.         Mov    set_err,-1    ;yes, read error
  68.         Jmp    short status
  69. RFDone:     Ret
  70. ;---------------------------------------
  71. filter:     Mov    cx,NumBytes
  72.         Cmp    cx,0
  73.         If z    mov set_err,-1
  74.         Mov    si,o buffer
  75.         Inc    cx        ;al is one behind si. get last byte
  76. proc_l:     Lodsb
  77.         Cmp    al,0ah        ;0ah is linefeed, so another line
  78.         Jne    _loopl
  79.         Inc    w line_count_l        ;lines found
  80.         Cmp    line_count_l,0        ;may be 0FFFF+1 lines
  81.         If z    inc w line_count_h    ; so inc high byte
  82. _loopl:     Loop    proc_l
  83.         Or    ax,ax        ;reset flags
  84.         Ret
  85. ;---------------------------------------
  86. status:     Jc    close_file    ;keep loading buffer till EOF
  87.         Cmp    b EOF,-1
  88.         Jne    ret
  89. ;---------------------------------------
  90. close_file:    Pop    ax        ;take ret off of stack
  91.         Mov    ah,3eh        ;DOS:close file, BX=handle
  92.         Mov    bx,handle
  93.         Int    21h        ; returns AX=error code
  94. ;---------------------------------------
  95. quit:        Cmp    set_err,0
  96.         Je    normal        ;no error
  97.         Mov    dx,o err_level
  98.         Mov    ah,9        ;display -1 error
  99.         Int    21h
  100. crlf:        Mov    dx,o nwln    ;send cr,lf
  101.         Mov    ah,9
  102.         Int    21h
  103. exit:        Mov    ah,4ch
  104.         Mov    al,set_err
  105.         Int    21h        ;exit w/errorlevel
  106.  
  107. normal:     Call    dec_32        ;display number of lines
  108.         Jmp    short crlf
  109. ;---------------------------------------
  110. dec_32:     Mov    bx,line_count_h
  111.         Cmp    bx,0        ;won't be zero if even 1 line (char10)
  112.         Je    no_line
  113.         Dec    bx        ;first one don't count
  114.         Mov    ax,line_count_l
  115.         Jmp    short DNUMP
  116.  
  117. no_line:    Mov    ax,0        ;no lines, but display "0"
  118.  
  119. ;-------------------------------------------------------------;
  120. ;         Richard Pavlicek <pavlicek@gate.net>          ;
  121. ;                                  ;
  122. ;Below is my routine, which displays (in decimal) the 32-bit  ;
  123. ;number in bx:ax.  For a 16-bit number, simply set bx to zero.;
  124. ;-------------------------------------------------------------;
  125. ;      Display number | bx high word, ax low word          ;
  126. ;-------------------------------------------------------------;
  127.  
  128. DNUMP:    mov    cx,0ah        ;divisor and flag
  129.     push    cx        ;put flag on stack
  130.  
  131. ;- divide & store digit loop
  132. DNUM1:    xchg    ax,bx        ;make ax high word, bx low word
  133.     sub    dx,dx        ;extend high word
  134.     div    cx        ;dx = remainder, ax high quotient
  135.     xchg    ax,bx        ;save high quotient, get low word
  136.     div    cx        ;dx = digit (0-9), ax low quotient
  137.     push    dx        ;save digit (remainder) on stack
  138.     mov    dx,bx        ;copy so not destroyed below
  139.     or    dx,ax        ;is full quotient zero?
  140.     jnz    DNUM1        ;no, continue
  141.  
  142.     pop    dx        ;get most significant digit
  143.  
  144. ;- display loop
  145. DNUM2:    or    dl,30h        ;convert to ASCII digit
  146.     mov    ah,02h        ;DOS display character function
  147.     int    21h        ;display it
  148.     pop    dx        ;get next digit or flag
  149.     cmp    dx,cx        ;is it a digit (less than 10)?
  150.     jc    DNUM2        ;yes, continue display
  151.     ret            ;no, flag met (stack normal)
  152. ;-------------------------------------------------------------;
  153. FilePtrH    Dw    0
  154. FilePtrL    Dw    0
  155. NumBytes    Dw    0
  156. handle        Dw    0
  157. line_count_l    Dw    0ffffh
  158. line_count_h    Dw    0
  159. EOF        Db    0
  160. set_err     Db    0
  161. err_level    Db    '-1$'
  162. nwln        Db    0dh,0ah,24h
  163. buffer        Db
  164.